package com.jiantong.web.open.app.lawEnforcing;

import com.jiantong.lawEnforcing.dto.SimplenessCasesDto;
import com.jiantong.lawEnforcing.model.SimplenessCases;
import com.jiantong.lawEnforcing.service.SimplenessCasesService;
import com.jiantong.web.core.enums.ExceptionCode;
import io.terminus.boot.rpc.common.annotation.RpcConsumer;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import io.terminus.pampas.openplatform.annotations.OpenBean;
import io.terminus.pampas.openplatform.annotations.OpenMethod;
import io.terminus.pampas.openplatform.exceptions.OPClientException;
import io.terminus.pampas.openplatform.exceptions.OPServerException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * 当场处罚决定书
 * Created by mz03 on 2017/8/15.
 */
@Slf4j
@OpenBean
public class SimplenessCasesApis {
    @RpcConsumer
    private SimplenessCasesService simplenessCasesService;


    /**
     * 根据id查询一条信息
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.simplenessCases.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public SimplenessCases findById(Long id){
        Response<SimplenessCases> response = simplenessCasesService.finById(id);
        if (!response.isSuccess()){
            log.error("failed to find simplenessCases read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 根据id查询一条当场处罚单DTO
     * @param id
     * @return
     */
    @OpenMethod(
            key = "find.simplenessCasesDto.by.id",
            paramNames = {"id"},
            httpMethods = RequestMethod.GET
    )
    public SimplenessCasesDto findSimplenessCasesDto(Long id){
        Response<SimplenessCasesDto> response = simplenessCasesService.findSimplenessCasesDto(id);
        if (!response.isSuccess()){
            log.error("failed to find SimplenessCasesDto read by id = {}, cause : {}", id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 根据cases_basic_id查询一条当场处罚单DTO
     * @param cases_basic_id
     * @return
     */
    @OpenMethod(
            key = "find.simplenessCasesDto.by.cases_basic_id",
            paramNames = {"cases_basic_id"},
            httpMethods = RequestMethod.GET
    )
    public SimplenessCasesDto findByCases_basic_id(Long cases_basic_id){
        Response<SimplenessCasesDto> response = simplenessCasesService.findByCases_basic_id(cases_basic_id);
        if (!response.isSuccess()){
            log.error("failed to find SimplenessCasesDto read by cases_basic_id = {}, cause : {}", cases_basic_id, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }


    /**
     * 创建一条信息
     * @param simplenessCases
     * @return
     */
    @OpenMethod(
            key = "create.simplenessCases",
            paramNames = {"simplenessCases"},
            httpMethods = RequestMethod.POST
    )
    public Long create(SimplenessCases simplenessCases){
        if (simplenessCases.getStatus()==null){
            simplenessCases.setStatus(1);
        }
        Response<Long> response = simplenessCasesService.create(simplenessCases);
        if(!response.isSuccess()){
            log.error("failed to create simplenessCases read by simplenessCases = {}, cause : {}", simplenessCases, response.getError());
            throw new OPServerException(response.getError());
        }
        return response.getResult();
    }

    /**
     * 更新单据
     * @param simplenessCases
     * @return
     */
    @OpenMethod(
            key = "update.simplenessCases",
            paramNames = {"simplenessCases"},
            httpMethods = RequestMethod.PUT
    )
    public Boolean update(SimplenessCases simplenessCases){
        if(simplenessCases.getId() == null){
            log.error("id should be not null");
            throw new OPClientException(ExceptionCode.ID_NOT_EXIST.value(),
                    "id should be not null");
        }
        Response<Boolean> response = simplenessCasesService.update(simplenessCases);
        if(!response.isSuccess()){
            log.error("failed to update simplenessCases");
            throw new OPServerException(ExceptionCode.DATA_UPDATE_FAIL.value(),
                    "failed to update simplenessCases");
        }
        return response.getResult();
    }


    /**
     * 根据条件分页查询
     * @param pageNo
     * @param pageSize
     * @param status
     * @param cause_action
     * @param cases_source
     * @param parties_type
     * @param parties_name
     * @return
     */
    @OpenMethod(
            key = "find.paging.simplenessCases",
            paramNames = {"pageNo","pageSize","status","cause_action","cases_source","parties_type","parties_name"},
            httpMethods = RequestMethod.GET
    )
    public Paging<SimplenessCases> pagingCasesBasic(
            Integer pageNo,
            Integer pageSize,
            Integer status,
            String cause_action,
            Integer cases_source,
            Integer parties_type,
            String parties_name
    ){
        Response<Paging<SimplenessCases>> pagingResponse =simplenessCasesService.pagingByStatus(pageNo,pageSize,status,cause_action,cases_source,parties_type,parties_name);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging casesBasic status {}, cause : {}", status, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }

    /**
     * 根据status分页查询 当场处罚决定书+案件信息+当事人信息 dto
     * @param pageNo
     * @param pageSize
     * @param status
     * @param cause_action
     * @param cases_source
     * @param parties_type
     * @param parties_name
     * @return
     */
    @OpenMethod(
            key = "find.paging.simplenessCasesDto",
            paramNames = {"pageNo","pageSize","status","cause_action","cases_source","parties_type","name"},
            httpMethods = RequestMethod.GET
    )
    public Paging<SimplenessCasesDto> pagingsimplenessCasesDto(
            Integer pageNo,
            Integer pageSize,
            Integer status,
            String cause_action,
            Integer cases_source,
            Integer parties_type,
            String parties_name
    ){
        Response<Paging<SimplenessCasesDto>> pagingResponse =simplenessCasesService.pagingSimplenessCasesDto(
                pageNo,pageSize,status,cause_action,cases_source,parties_type,parties_name);
        if(!pagingResponse.isSuccess()){
            log.error("failed to find paging simplenessCasesDto status {}, cause : {}", status, pagingResponse.getError());
            throw new OPServerException(pagingResponse.getError());
        }
        return pagingResponse.getResult();
    }

}
